package sdk

import (
	"fabric-go-sdk/sdk/application"
	"fabric-go-sdk/sdk/sdkInfo/chaincodeInfo"
	"fabric-go-sdk/sdk/sdkInfo/channelInfo"
	"fabric-go-sdk/sdk/sdkInfo/ordererInfo"
	"fabric-go-sdk/sdk/sdkInfo/orgInfo"
	"fmt"
	"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
	"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
	"os"
)

var App application.Application

func InitSdk() {
	// 初始化已经启动的网络信息
	org := orgInfo.New("Org1", "Org1MSP", []string{"peer0.org1.example.com", "peer1.org1.example.com"}, []string{"User1"}, "Admin", "fixtures/channel-artifacts/Org1MSPanchors.tx")
	orderer := ordererInfo.New("OrdererOrg", "Admin", []string{"orderer.example.com"})
	chaincode1 := chaincodeInfo.NewAlreadyRunningChaincode("simplecc", "./chaincode/simplecc/", "1.0.0", 1, "simplecc_1.0.0")
	chaincode2 := chaincodeInfo.NewAlreadyRunningChaincode("mycc", "./chaincode/mycc/", "1.0", 1, "mycc_1.0")
	// 通道信息1
	channel1 := channelInfo.New("mychannel", "fixtures/channel-artifacts/channel.tx")
	channel1.AddOrg(org.OrgName, org)
	channel1.AddChainCode(chaincode1.Name, chaincode1)
	channel1.AddChainCode(chaincode2.Name, chaincode2)
	channel1.SetDefaultParams()
	// 通道信息2
	channel2 := channelInfo.New("testchannel", "fixtures/channel-artifacts/testchannel.tx")
	channel2.AddOrg(org.OrgName, orgInfo.New("Org1", "Org1MSP", []string{}, []string{"User1"}, "Admin", "fixtures/channel-artifacts/Org1MSPanchors.tx"))
	channel2.SetDefaultParams()
	// 创建fabricSDK实例   读取config.yaml文件
	FabSDK, err := fabsdk.New(config.FromFile("sdk/config.yaml"))
	if err != nil {
		fmt.Println("  >> SDK设置失败:", err)
		os.Exit(-1)
	}
	// 实例化Application
	App = application.Application{
		Channels: map[string]*channelInfo.ChannelInfo{channel1.ChannelID: channel1, channel2.ChannelID: channel2},
		Orderer:  orderer,
		FabSDK:   FabSDK,
	}
	fmt.Println("  >> 初始化客户端成功")
}
